package com.skins 
{
	import com.skins.Skin;
	
	import flash.display.DisplayObject;
	import flash.text.TextFormat;
	import flash.utils.describeType;
	
	import cn.flashk.controls.Button;
	import cn.flashk.controls.CheckBox;
	import cn.flashk.controls.Label;
	import cn.flashk.controls.RadioButton;
	import cn.flashk.controls.SkinButton;
	import cn.flashk.controls.TextInput;
	import cn.flashk.controls.support.UIComponent;

	/**
	 * ...
	 * @author Johnny Cheng
	 */
	public class SkinSetManager 
	{
		/**
		 * 设置组件的皮肤
		 * @param	component 要设置皮肤的组件
		 * @param	skinID 组件皮肤ID
		 * @return 如果设置成功返回true，否则返回false
		 */
		static public function setSkin(component:Object, skinID:String):Boolean {
			if (component is UIComponent) {
				if (component is Label) setFlLabelSkin(component as UIComponent, skinID);
				else if (component is TextInput) setFlTextInputSkin(component as UIComponent, skinID);
				else if (component is Button) setFlButtonSkin(component as UIComponent, skinID);
				else if (component is SkinButton) setFlButtonSkin(component as UIComponent, skinID);
				else if (component is CheckBox) setFlButtonSkin(component as UIComponent, skinID);
				else if (component is RadioButton) setFlRadioButtonSkin(component as UIComponent, skinID);
				else return false;
				return true;
			}
			if (describeType(component).method.(@name == "setSkinID").length() > 0) {
				component.setSkinID(skinID);
				return true;
			}
			
			return false;
		}
		
		static private function setFlLabelSkin(component:UIComponent, skinID:String):void {
			var upTextFormat:TextFormat = Skin.librarys.getTextFormat(skinID, "upTxt");
			var disabledTextFormat:TextFormat = Skin.librarys.getTextFormat(skinID, "disabledTxt");
			component.setStyle("textFormat", upTextFormat);
			component.setStyle("disabledTextFormat", disabledTextFormat);
		}
		
		/**
		 * 设置输入框样式及背景
		 * @param component
		 * @param skinID
		 */		
		static private function setFlTextInputSkin(component:UIComponent, skinID:String):void {
			var bg:DisplayObject = Skin.librarys.getSkin(skinID, "bg");
			component.setStyle("upSkin", bg);
			component.setStyle("disabledSkin", bg);
			
			setFlLabelSkin(component, skinID);
		}
		
		/**
		 * 设置按钮内容
		 * @param component
		 * @param skinID
		 */		
		static private function setFlButtonSkin(component:UIComponent, skinID:String):void {
			var upBg:DisplayObject = Skin.librarys.getSkin(skinID, "up");
            var overBg:DisplayObject = Skin.librarys.getSkin(skinID, "over");
            var downBg:DisplayObject = Skin.librarys.getSkin(skinID, "down");
            var disabledBg:DisplayObject = Skin.librarys.getSkin(skinID, "disabled");
			if (overBg == null) overBg = upBg;
			if (downBg == null) downBg = overBg;
			if (disabledBg == null) disabledBg = downBg;
			component.setStyle("upSkin", upBg);
			component.setStyle("overSkin", overBg);
			component.setStyle("downSkin", downBg);
			component.setStyle("disabledSkin", disabledBg);
			
			setFlLabelSkin(component, skinID);
		}
		
		static private function setFlCheckBoxSkin(component:UIComponent, skinID:String):void
		{
			var upBg:DisplayObject = Skin.librarys.getSkin(skinID, "up");
            var overBg:DisplayObject = Skin.librarys.getSkin(skinID, "over");
            var downBg:DisplayObject = Skin.librarys.getSkin(skinID, "down");
            var disabledBg:DisplayObject = Skin.librarys.getSkin(skinID, "disabled");
			if (overBg == null) overBg = upBg;
			if (downBg == null) downBg = overBg;
			if (disabledBg == null) disabledBg = downBg;
			
			var selectedUpBg:DisplayObject = Skin.librarys.getSkin(skinID, "up");
            var selectedOverBg:DisplayObject = Skin.librarys.getSkin(skinID, "over");
            var selectedDownBg:DisplayObject = Skin.librarys.getSkin(skinID, "down");
            var selectedDisabledBg:DisplayObject = Skin.librarys.getSkin(skinID, "disabled");
			if (selectedOverBg == null) selectedOverBg = selectedUpBg;
			if (selectedDownBg == null) selectedDownBg = selectedOverBg;
			if (selectedDisabledBg == null) selectedDisabledBg = selectedDownBg;
			
			component.setStyle("upIcon", upBg);
			component.setStyle("overIcon", overBg);
			component.setStyle("downIcon", downBg);
			component.setStyle("disabledIcon", disabledBg);
			component.setStyle("selectedUpIcon", selectedUpBg);
			component.setStyle("selectedOverIcon", selectedOverBg);
			component.setStyle("selectedDownIcon", selectedDownBg);
			component.setStyle("selectedDisabledIcon", selectedDisabledBg);
			
			setFlLabelSkin(component, skinID);
		}
		
		static private function setFlRadioButtonSkin(component:UIComponent, skinID:String):void
		{
			setFlCheckBoxSkin(component, skinID);
		}
	}

}